home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / sysdeps / mach / hurd / bind.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-30  |  3.2 KB  |  103 lines

  1. /* Copyright (C) 1992, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <sys/socket.h>
  22. #include <hurd.h>
  23. #include <hurd/fd.h>
  24. #include <hurd/socket.h>
  25. #include <hurd/paths.h>
  26. #include <fcntl.h>
  27. #include <stddef.h>
  28. #include <hurd/ifsock.h>
  29. #include <sys/un.h>
  30. #include <string.h>
  31.  
  32. /* Give the socket FD the local address ADDR (which is LEN bytes long).  */
  33. int
  34. DEFUN(bind, (fd, addr, len),
  35.       int fd AND struct sockaddr *addr AND size_t len)
  36. {
  37.   addr_port_t aport;
  38.   error_t err;
  39.  
  40.   if (addr->sa_family == AF_LOCAL)
  41.     {
  42.       /* For the local domain, we must create a node in the filesystem
  43.      using the ifsock translator and then fetch the address from it.  */
  44.       struct sockaddr_un *unaddr = (struct sockaddr_un *) addr;
  45.       file_t dir, node;
  46.       char name[len - offsetof (struct sockaddr_un, sun_path)], *n;
  47.       strncpy (name, unaddr->sun_path, sizeof name);
  48.       dir = __file_name_split (name, &n);
  49.       if (dir == MACH_PORT_NULL)
  50.     return -1;
  51.       
  52.       /* Create a new, unlinked node in the target directory.  */
  53.       err = __dir_mkfile (dir, O_CREAT, 0666 & ~_hurd_umask, &node);
  54.  
  55.       if (! err)
  56.     {
  57.       file_t ifsock;
  58.       /* Set the node's translator to make it a local-domain socket.  */
  59.       err = __file_set_translator (node, 
  60.                        FS_TRANS_EXCL | FS_TRANS_SET,
  61.                        FS_TRANS_EXCL | FS_TRANS_SET, 0,
  62.                        _HURD_IFSOCK, sizeof _HURD_IFSOCK,
  63.                        MACH_PORT_NULL,
  64.                        MACH_MSG_TYPE_COPY_SEND);
  65.       if (! err)
  66.         /* Get a port to the ifsock translator.  */
  67.         err = __hurd_invoke_translator (node, 0, &ifsock);
  68.       if (! err)
  69.         /* Get the address port.  */
  70.         err = __ifsock_getsockaddr (ifsock, &aport);
  71.       __mach_port_deallocate (__mach_task_self (), ifsock);
  72.       if (! err)
  73.         /* Link the node, now a socket, into the target directory.  */
  74.         err = __dir_link (node, dir, name);
  75.       __mach_port_deallocate (__mach_task_self (), node);
  76.     }
  77.       __mach_port_deallocate (__mach_task_self (), dir);
  78.  
  79.       if (err)
  80.     return __hurd_fail (err);
  81.     }
  82.   else
  83.     err = EIEIO;
  84.  
  85.   err = HURD_DPORT_USE (fd,
  86.             ({
  87.               if (err)
  88.                 err = __socket_create_address (port,
  89.                                addr->sa_family,
  90.                                (char *) addr, len,
  91.                                &aport, 1);
  92.               if (! err)
  93.                 {
  94.                   err = __socket_bind (port, aport);
  95.                   __mach_port_deallocate (__mach_task_self (),
  96.                               aport);
  97.                 }
  98.               err;
  99.             }));
  100.  
  101.   return err ? __hurd_dfail (fd, err) : 0;
  102. }
  103.